//
//  DJNavigationController.swift
//  DJWeibo
//
//  Created by demo on 2017/2/7.
//  Copyright © 2017年 demo. All rights reserved.
//

import UIKit

class DJNavigationController: UINavigationController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        //隐藏默认的navigationBar
        navigationBar.isHidden = true

        // Do any additional setup after loading the view.
    }

    override func pushViewController(_ viewController: UIViewController, animated: Bool) {
        print(viewController)
        
        //如果不是根控制器才需要隐藏，根控制器不需要处理
        if childViewControllers.count > 0 {
            //根控制器的子控制器count为0
            
            //隐藏底部的tabbar
            viewController.hidesBottomBarWhenPushed = true
        
        //判断控制器类型
        if let vc = viewController as? DJBaseViewController{
            
            var title = "返回"
            
            //判断控制器的级数
            if childViewControllers.count == 1 {
                title = childViewControllers.first?.title ?? "返回"
            }
            
            //取出自定义的navItem
            vc.navItem.leftBarButtonItem = UIBarButtonItem(title: title, target: self, action: #selector(popToParent),isBack:true)
        }
    }
        super.pushViewController(viewController, animated: true)
        //super必须放在后面
    }
    
    @objc fileprivate func popToParent(){
        popViewController(animated: true)
    }
}
